

#import "MailEditViewController.h"
#import "MailEditCell.h"
#import "TextViewCell.h"
#import "IDominoAppDelegate.h"
@implementation MailEditViewController


@synthesize myTableView, toTextField, ccTextField, bccTextField, subjectTextField, textView;


- (id)init
{
	self = [super init];
	if (self)
	{
		textView = [[UITextView alloc] initWithFrame:CGRectMake(0.0, 140.0, 320.0, 460-140)]  ;
		textView.font=[UIFont systemFontOfSize:16 ];
		textView.autoresizingMask=UIViewAutoresizingFlexibleHeight;
		textView.scrollEnabled=NO;
		
		toTextField=[[UITextField alloc] initWithFrame:CGRectMake(65.0, 5.0, 270.0, 25.0)];
		ccTextField=[[UITextField alloc] initWithFrame:CGRectMake(45.0, 5.0, 270.0, 25.0)];
		bccTextField=[[UITextField alloc] initWithFrame:CGRectMake(45.0, 5.0, 270.0, 25.0)];
		subjectTextField=[[UITextField alloc] initWithFrame:CGRectMake(75.0, 5.0, 270.0, 25.0)];
		toTextField.font=[UIFont systemFontOfSize:20];
		toTextField.delegate=self;
		toTextField.returnKeyType=UIReturnKeyNext;
		toTextField.autocorrectionType=UITextAutocorrectionTypeNo;
		ccTextField.font=[UIFont systemFontOfSize:20];
		ccTextField.delegate=self;
		ccTextField.returnKeyType=UIReturnKeyNext;
		ccTextField.text=@"";
		
		ccTextField.autocorrectionType=UITextAutocorrectionTypeNo;
		bccTextField.font=[UIFont systemFontOfSize:20];
		bccTextField.delegate=self;
		bccTextField.returnKeyType=UIReturnKeyNext;	
		bccTextField.autocorrectionType=UITextAutocorrectionTypeNo;
		bccTextField.text=@"";
		subjectTextField.font=[UIFont systemFontOfSize:20];
		subjectTextField.delegate=self;
		subjectTextField.returnKeyType=UIReturnKeyNext;	
		subjectTextField.text=@"";
		self.title=NSLocalizedString(@"NewMail", @"New Mail");
		
		IDominoAppDelegate *appDelegate = (IDominoAppDelegate *)[[UIApplication sharedApplication] delegate];
		UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:	UIBarButtonSystemItemCancel
																				target:self action:@selector(cancelAction:)];
		UIBarButtonItem *sendItem = [[UIBarButtonItem alloc] initWithTitle:	@"Send" style:UIBarButtonItemStyleDone
																	target:appDelegate action:@selector(sendAction:)];

		self.navigationItem.leftBarButtonItem = cancelItem;
		self.navigationItem.rightBarButtonItem = sendItem;
		[cancelItem release];
		[sendItem release];

		

	}
	
	return self;
}

- (void)loadView
{//[[UIScreen mainScreen] applicationFrame]
	myTableView = [[UITableView alloc] initWithFrame: CGRectMake(0,0,320,210) style:UITableViewStylePlain];	
	myTableView.delegate = self;
	myTableView.dataSource = self;
	myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
	myTableView.autoresizingMask=UIViewAutoresizingNone;
	myTableView.rowHeight=35;
	self.view =myTableView;
	myTableView.tableFooterView= textView;
}

- (void)viewWillAppear:(BOOL)animated
{

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) 
												 name:UIKeyboardDidShowNotification object:nil]; 
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardDidHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil]; 
	[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil]; 
}

- (void)dealloc
{
	[myTableView release];
	[super dealloc];
}



#pragma mark - UITableView delegates

// if you want the entire table to just be re-orderable then just return UITableViewCellEditingStyleNone
//
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return UITableViewCellEditingStyleNone;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return 4;
}


// utility routine leveraged by 'cellForRowAtIndexPath' to determine which UITableViewCell to be used on a given row.
//
- (UITableViewCell *)obtainTableCellForRow:(NSInteger)row
{
	UITableViewCell *cell=[[[MailEditCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"editLabelCell"] autorelease];
	return cell;
}

// to determine which UITableViewCell to be used on a given row.
//
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	IDominoAppDelegate *appDelegate = (IDominoAppDelegate *)[[UIApplication sharedApplication] delegate];
	NSInteger row = [indexPath row];
	UITableViewCell *sourceCell = [self obtainTableCellForRow:row];
	UIFont* LabelFont=[UIFont systemFontOfSize:20];
	NSString* title;
	int width;
	UILabel* label;
	if( row == 0 )
	{
		title=NSLocalizedString(@"To:", @"To:");
		width=[title  sizeWithFont:LabelFont].width+10;
		label=[[UILabel alloc] initWithFrame:CGRectMake(5.0, 5.0,width, 25.0)];
		label.font=LabelFont;
		toTextField.frame=CGRectMake(width+5, 5.0, 300-width-10, 25.0);
		[label setText:title];
		toButton=[UIButton buttonWithType:UIButtonTypeContactAdd];
		[toButton addTarget:appDelegate action:@selector(selectEmailFromDatabase) forControlEvents:UIControlEventTouchDown];
		[((MailEditCell *)sourceCell) setTextField:toTextField label:label button:toButton];
		[label release];
	}
	else if (row == 1)
	{
		title=NSLocalizedString(@"Cc:", @"Cc:");
		width=[title  sizeWithFont:LabelFont].width+10;
		label=[[UILabel alloc] initWithFrame:CGRectMake(5.0, 5.0, width, 25.0)];
		label.font=LabelFont;
		ccTextField.frame=CGRectMake(width+5, 5.0, 300-width-5-10, 25.0);
		[label setText:title];
		ccButton=[UIButton buttonWithType:UIButtonTypeContactAdd];
		[ccButton addTarget:appDelegate action:@selector(selectEmailFromDatabase) forControlEvents:UIControlEventTouchDown];
		[((MailEditCell *)sourceCell) setTextField:ccTextField label:label button:ccButton];
		[label release];
	}
	else if (row == 2)
	{
		title=NSLocalizedString(@"Bcc:", @"Bcc:");
		width=[title  sizeWithFont:LabelFont].width+10;
		label=[[UILabel alloc] initWithFrame:CGRectMake(5.0, 5.0, width, 25.0)];
		bccTextField.frame=CGRectMake(width+5.0, 5.0, 300-width-5-10, 25.0);
		[label setText:title];
		label.font=LabelFont;
		bccButton=[UIButton buttonWithType:UIButtonTypeContactAdd];
		[bccButton addTarget:appDelegate action:@selector(selectEmailFromDatabase) forControlEvents:UIControlEventTouchDown];
		[((MailEditCell *)sourceCell) setTextField:bccTextField label:label button:bccButton];
		[label release];
	}
	else if (row == 3)
	{
		title=NSLocalizedString(@"Subject:", @"subject:");
		width=[title  sizeWithFont:LabelFont].width+10;
		label=[[UILabel alloc] initWithFrame:CGRectMake(5.0, 5.0, width, 25.0)];
		subjectTextField.frame=CGRectMake(width+5.0, 5.0, 300-width-5, 25.0);
		[label setText:title];
		label.font=LabelFont;
		[((MailEditCell *)sourceCell) setTextField:subjectTextField label:label button:nil];
		[label release];
	}
	return sourceCell;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
	if(textField==toTextField)
		toButton.hidden=NO;
	else if(textField==ccTextField)
		ccButton.hidden=NO;
	else if(textField==bccTextField)
		bccButton.hidden=NO;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
	if(textField==toTextField){
		toButton.hidden=YES;
	}
	else if(textField==ccTextField)
		ccButton.hidden=YES;
	else if(textField==bccTextField)
		bccButton.hidden=YES;
	else if(textField==subjectTextField)
		self.navigationItem.title=subjectTextField.text;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	if(textField==toTextField)
		[ccTextField becomeFirstResponder];
	else if(textField==ccTextField)
		[bccTextField becomeFirstResponder];
	else if(textField==bccTextField)
		[subjectTextField becomeFirstResponder];
	else if(textField==subjectTextField)
		[textView becomeFirstResponder];
	return YES;
}

- (void)cancelAction:(id)sender
{
	UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
															 delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Keep" otherButtonTitles:@"Discard",nil];
	actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
	if([textView isFirstResponder])
		[textView resignFirstResponder];
	[self.myTableView setFrame:CGRectMake(0, 0, 320, 460)];
	[actionSheet becomeFirstResponder];
	[actionSheet showInView:self.view]; // show from our table view (pops up in the middle of the table)
	[actionSheet release];

}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
	if(buttonIndex==0){
		[[self navigationController] popViewControllerAnimated:YES];
	}
	else if(buttonIndex==1){
		[self clearFields];
		[[self navigationController] popViewControllerAnimated:YES];
	}
}

- (void)clearFields
{
	self.toTextField.text=@"";
	self.ccTextField.text=@"";
	self.bccTextField.text=@"";
	self.subjectTextField.text=@"";
	self.textView.text=@"";
}





- (void)keyboardWasShown:(NSNotification*)aNotification

{
	[self.myTableView setFrame:CGRectMake(0, 0, 320, 200)];
}

- (void)keyboardWasHidden:(NSNotification*)aNotification
{	
	
}

@end
